package com.rc.saas.tenant.controller.invoice;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.redis.RedisLoader;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.utils.StringUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.controller.banner.TenantBannerController;
import com.rc.saas.tenant.model.hlht.HlhtOperator;
import com.rc.saas.tenant.model.hlht.HlhtOperatorExample;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpen;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpenExample;
import com.rc.saas.tenant.model.invoice.TenantInvoiceInfo;
import com.rc.saas.tenant.model.tenant.Tenant;
import com.rc.saas.tenant.model.tenant.TenantExample;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.elec.StationInvoiceSearch;
import com.rc.saas.tenant.service.hlht.HlhtOperatorService;
import com.rc.saas.tenant.service.hlht.HlhtTenantOpenService;
import com.rc.saas.tenant.service.invoice.TenantInvoiceInfoService;
import com.rc.saas.tenant.service.tenant.TenantService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.invoice.StationInvoiceDetailExportVo;
import com.rc.saas.tenant.vo.invoice.StationInvoiceExportVo;
import com.rc.saas.tenant.vo.invoice.StationInvoiceVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.*;


/**
 * @author frog
 * @date 2021/4/14
 */
@Controller
@RequestMapping("/tenantInvoiceInfo")
public class TenantInvoiceInfoController {
    private static Logger logger = LogManager.getLogger(TenantBannerController.class);


    @Autowired
    private TenantInvoiceInfoService tenantInvoiceInfoService;

    @Autowired
    private RedisLoader redisLoader;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private HlhtTenantOpenService hlhtTenantOpenService;

    @Autowired
    private HlhtOperatorService hlhtOperatorService;

    /**
     * 跳转电子发票信息页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/templates/invoice/tenantInvoiceInfo";
    }


    /**
     * 返回电子发票信息列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageTenantInvoiceInfo", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageTenantInvoiceInfo(BaseSearch baseSearch) {
        PageInfo<TenantInvoiceInfo> pageInfo = tenantInvoiceInfoService.listPageTenantInvoiceInfo(baseSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 跳转增加电子发票信息页面
     *
     * @return
     */
    @RequestMapping(value = "/addTenantInvoiceInfo", method = RequestMethod.GET)
    public String addTenantInvoiceInfo() {
        return "/templates/invoice/addTenantInvoiceInfo";
    }

    /**
     * 跳转编辑电子发票信息页面
     *
     * @return
     */
    @RequestMapping(value = "/editTenantInvoiceInfo/{tenantCode}", method = RequestMethod.GET)
    public String editTenantInvoiceInfo(@PathVariable("tenantCode") String tenantCode, Model model) {
        TenantInvoiceInfo tenantInvoiceInfo = tenantInvoiceInfoService.selectByPrimaryKey(tenantCode);
        model.addAttribute("tenantInvoiceInfo", tenantInvoiceInfo);
        return "/templates/invoice/editTenantInvoiceInfo";
    }

    /**
     * 保存/编辑电子发票信息
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateTenantInvoiceInfo", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateTenantInvoiceInfo(TenantInvoiceInfo tenantInvoiceInfo) {
        if (!BeanUtil.isValid(tenantInvoiceInfo.getTenantCode(), tenantInvoiceInfo.getSellerBank(), tenantInvoiceInfo.getSellerTel()
                , tenantInvoiceInfo.getSellerAddress(), tenantInvoiceInfo.getSellerName(), tenantInvoiceInfo.getSellerCode(),
                tenantInvoiceInfo.getPayee(), tenantInvoiceInfo.getReviewer(), tenantInvoiceInfo.getDrawer(), tenantInvoiceInfo.getRemindMobiles())) {
            return TResponse.FAIL("请输入正确的参数");
        }
        try {
            TenantInvoiceInfo template = tenantInvoiceInfoService.selectByPrimaryKey(tenantInvoiceInfo.getTenantCode());
            if (template != null) {
                tenantInvoiceInfoService.updateByPrimaryKeySelective(tenantInvoiceInfo);
            } else {
                tenantInvoiceInfoService.insertSelective(tenantInvoiceInfo);
            }

            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存电子发票信息失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 删除电子发票信息
     *
     * @return
     */
    @RequestMapping(value = "/deleteTenantInvoiceInfo", method = RequestMethod.POST)
    @ResponseBody
    public TResponse deleteTenantInvoiceInfo(String tenantCode) {
        try {
            tenantInvoiceInfoService.deleteByPrimaryKey(tenantCode);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("删除电子发票信息", e);
            return TResponse.FAIL("操作失败");
        }
    }

    /**
     * 跳转至 桩群发票页面
     * @return
     */
    @RequestMapping(value = "/stationInvoice", method = RequestMethod.GET)
    public String stationInvoice(Model model){
        TenantUser tenantUser=ShiroUtils.getSessionUser();
        Tenant tenant=tenantService.selectByPrimaryKey(tenantUser.getTenantCode());
        Set<String> tenantCodes = new HashSet();
        List operatorIds = new LinkedList();
        HlhtTenantOpenExample tenantExample1 = new HlhtTenantOpenExample();
        tenantExample1.createCriteria().andTenantCodeEqualTo(tenantUser.getTenantCode()).andStatusEqualTo("1").andHlhtTypeEqualTo("1");
        List<HlhtTenantOpen> hlhtTenantOpens = hlhtTenantOpenService.selectByExample(tenantExample1);

        for (HlhtTenantOpen hlhtTenantOpen : hlhtTenantOpens) {
            if (StringUtil.isNotBlank(hlhtTenantOpen.getHlhtTenantCode())) {
                tenantCodes.add(hlhtTenantOpen.getHlhtTenantCode());
            }
            if (StringUtil.isNotBlank(hlhtTenantOpen.getHlhtOperatorId())) {
                operatorIds.add(hlhtTenantOpen.getHlhtOperatorId());
            }
        }

        HlhtTenantOpenExample tenantExample2 = new HlhtTenantOpenExample();
        tenantExample2.createCriteria().andHlhtTenantCodeEqualTo(tenantUser.getTenantCode()).andStatusEqualTo("1").andHlhtTypeEqualTo("0");
        List<HlhtTenantOpen> hlhtTenantOpens1 = hlhtTenantOpenService.selectByExample(tenantExample2);

        for (HlhtTenantOpen hlhtTenantOpen : hlhtTenantOpens1) {
            tenantCodes.add(hlhtTenantOpen.getTenantCode());
        }
        List<String> result = new ArrayList<>(tenantCodes);
        List<Tenant> tenantList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(result)) {
            TenantExample tenantExample = new TenantExample();
            tenantExample.createCriteria().andTenantCodeIn(result);
            tenantList = tenantService.selectByExample(tenantExample);
        }

        List<HlhtOperator> hlhtOperators = new ArrayList<>();

        if (!CollectionUtils.isEmpty(operatorIds)) {
            HlhtOperatorExample example = new HlhtOperatorExample();
            example.createCriteria().andHlhtOperatorIdIn(operatorIds);
            hlhtOperators = hlhtOperatorService.selectByExample(example);
        }
        model.addAttribute("tenant", tenant);
        model.addAttribute("tenantList", tenantList);
        model.addAttribute("hlhtOperators", hlhtOperators);
        return "/templates/invoice/stationInvoice";
    }

    /**
     * 桩群发票统计信息
     * @param stationInvoiceSearch
     */
    @ResponseBody
    @RequestMapping(value = "/listPageCountStationInvoice", method = RequestMethod.GET)
    public WebPageInfo listPageCountStationInvoice(StationInvoiceSearch stationInvoiceSearch){
        TenantUser user = ShiroUtils.getSessionUser();
        if (StringUtil.isNotBlank(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
            if ("tenantCode".equals(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
                stationInvoiceSearch.setUserTenantCode(user.getTenantCode());
            } else {
                HlhtOperator hlhtOperator = hlhtOperatorService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (hlhtOperator != null) {
                    stationInvoiceSearch.setEquipmentOperatorId(hlhtOperator.getHlhtOperatorId());
                }

                Tenant tenant = tenantService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (tenant != null) {
                    stationInvoiceSearch.setEquipmentTenantCode(tenant.getTenantCode());
                }
            }
        }
        PageInfo<StationInvoiceVo> pageInfo=new PageInfo<>();
        if(StringUtil.isNotBlank(stationInvoiceSearch.getOrderType())){
            //个人订单统计
            if ("0".equals(stationInvoiceSearch.getOrderType())){
                pageInfo = tenantInvoiceInfoService.listPageCountStationInVoice1(stationInvoiceSearch);
            } else if ("1".equals(stationInvoiceSearch.getOrderType())) {
                //企业订单统计
                pageInfo = tenantInvoiceInfoService.listPageCountStationInvoice2(stationInvoiceSearch);
            } else if ("2".equals(stationInvoiceSearch.getOrderType())) {
                //互联订单统计
                pageInfo = tenantInvoiceInfoService.listPageCountStationInVoice(stationInvoiceSearch);
            }
        }
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

//    /**
//     * 桩群发票统计信息
//     * @param stationInvoiceSearch
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = {"/listPageCountStationInvoice"}, method = {RequestMethod.GET})
//    public WebPageInfo listPageCountStationInvoice(StationInvoiceSearch stationInvoiceSearch) {
//        PageInfo<StationInvoiceVo> pageInfo = this.tenantInvoiceInfoService.listPageCountStationInVoice(stationInvoiceSearch);
//        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
//        return webPageInfo;
//    }


    /**
     * 导出某个站点的详细发票详情
     * @param stationInvoiceSearch
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/exportStationInvoiceDetail", method = RequestMethod.GET)
    public TResponse exportStationInvoiceDetail(StationInvoiceSearch stationInvoiceSearch, HttpServletResponse response){
        TenantUser user = ShiroUtils.getSessionUser();
        ExportUtils.setDefaultPaging(stationInvoiceSearch);
        if (StringUtil.isNotBlank(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
            if ("tenantCode".equals(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
                stationInvoiceSearch.setUserTenantCode(user.getTenantCode());
            } else {
                HlhtOperator hlhtOperator = hlhtOperatorService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (hlhtOperator != null) {
                    stationInvoiceSearch.setEquipmentOperatorId(hlhtOperator.getHlhtOperatorId());
                }

                Tenant tenant = tenantService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (tenant != null) {
                    stationInvoiceSearch.setEquipmentTenantCode(tenant.getTenantCode());
                }
            }
        }
        List<StationInvoiceDetailExportVo> list=new ArrayList<>();
        if (StringUtil.isNotBlank(stationInvoiceSearch.getOrderType())){
            if ("0".equals(stationInvoiceSearch.getOrderType())){
                list=tenantInvoiceInfoService.listStationInvoiceDetail1(stationInvoiceSearch);
            } else if ("1".equals(stationInvoiceSearch.getOrderType())) {
                list=tenantInvoiceInfoService.listStationInvoiceDetail2(stationInvoiceSearch);
            } else if ("2".equals(stationInvoiceSearch.getOrderType())) {
                list=tenantInvoiceInfoService.listStationInvoiceDetail(stationInvoiceSearch);
            }
        }
//        List<StationInvoiceDetailExportVo> list =  tenantInvoiceInfoService.listStationInvoiceDetail(stationInvoiceSearch);

        response.setHeader("Set-Cookie", "fileDownload=true; path=/");

        if (list.size() > 150000) {
            return TResponse.FAIL("最多导出十五万数据");
        }

        if (list.size() >= 100000) {
            String exportData = (String) redisLoader.get("saas:exportData");
            if (exportData != null) {
                return TResponse.FAIL("导出人数过多,请稍后重试");
            }
//            TenantUser user = ShiroUtils.getSessionUser();
            redisLoader.set("saas:exportData", user.getTenantCode(), 1000 * 20);
        }

        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("桩群发票详情列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), StationInvoiceDetailExportVo.class).sheet("桩群发票详情列表").doWrite(list);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("导出桩群发票详情列表", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }

    }

    /**
     * 导出桩群发票
     * @param stationInvoiceSearch
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/exportStationInvoice", method = RequestMethod.GET)
    public TResponse exportStationInvoice(StationInvoiceSearch stationInvoiceSearch, HttpServletResponse response){
        TenantUser user = ShiroUtils.getSessionUser();
        ExportUtils.setDefaultPaging(stationInvoiceSearch);
        if (StringUtil.isNotBlank(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
            if ("tenantCode".equals(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
                stationInvoiceSearch.setUserTenantCode(user.getTenantCode());
            } else {
                HlhtOperator hlhtOperator = hlhtOperatorService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (hlhtOperator != null) {
                    stationInvoiceSearch.setEquipmentOperatorId(hlhtOperator.getHlhtOperatorId());
                }

                Tenant tenant = tenantService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (tenant != null) {
                    stationInvoiceSearch.setEquipmentTenantCode(tenant.getTenantCode());
                }
            }
        }
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        if (stationInvoiceSearch.getCount() == 0 || stationInvoiceSearch.getCount() == null) {
            stationInvoiceSearch.setCount(stationInvoiceSearch.getLimit());
        }


        if (stationInvoiceSearch.getCount() > 150000) {
            return TResponse.FAIL("最多导出十五万数据");
        }

        if (stationInvoiceSearch.getCount() >= 100000) {
            String exportData = (String) redisLoader.get("saas:exportData");
            if (exportData != null) {
                return TResponse.FAIL("导出人数过多,请稍后重试");
            }
            redisLoader.set("saas:exportData", user.getTenantCode(), 1000 * 20);
        }
        if(stationInvoiceSearch.getOffset() == null){
            stationInvoiceSearch.setOffset(0);
        }else {
            stationInvoiceSearch.setOffset(stationInvoiceSearch.getLimit() * (stationInvoiceSearch.getOffset() - 1));
        }
        List<StationInvoiceExportVo> list=new ArrayList<>();
        if (StringUtil.isNotBlank(stationInvoiceSearch.getOrderType())){
            if ("0".equals(stationInvoiceSearch.getOrderType())){
                list=tenantInvoiceInfoService.listCountStationInVoice1(stationInvoiceSearch);
            } else if ("1".equals(stationInvoiceSearch.getOrderType())) {
                list=tenantInvoiceInfoService.listCountStationInVoice2(stationInvoiceSearch);
            } else if ("2".equals(stationInvoiceSearch.getOrderType())) {
                list=tenantInvoiceInfoService.listCountStationInVoice(stationInvoiceSearch);
            }
        }
//        List<StationInvoiceExportVo> list =  tenantInvoiceInfoService.listCountStationInVoice(stationInvoiceSearch);


        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("桩群发票列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), StationInvoiceExportVo.class).sheet("桩群发票列表").doWrite(list);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("导出桩群发票列表", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }

    }

    /**
     * 跳转分销商桩群发票
     * @return
     */
    @RequestMapping(value = "/operatorStationInvoice", method = RequestMethod.GET)
    public String operatorStationInvoice(Model model){
        TenantUser tenantUser=ShiroUtils.getSessionUser();
        Tenant tenant=tenantService.selectByPrimaryKey(tenantUser.getTenantCode());
        Set<String> tenantCodes = new HashSet();
        List operatorIds = new LinkedList();
        HlhtTenantOpenExample tenantExample1 = new HlhtTenantOpenExample();
        tenantExample1.createCriteria().andTenantCodeEqualTo(tenantUser.getTenantCode()).andStatusEqualTo("1").andHlhtTypeEqualTo("1");
        List<HlhtTenantOpen> hlhtTenantOpens = hlhtTenantOpenService.selectByExample(tenantExample1);

        for (HlhtTenantOpen hlhtTenantOpen : hlhtTenantOpens) {
            if (StringUtil.isNotBlank(hlhtTenantOpen.getHlhtTenantCode())) {
                tenantCodes.add(hlhtTenantOpen.getHlhtTenantCode());
            }
            if (StringUtil.isNotBlank(hlhtTenantOpen.getHlhtOperatorId())) {
                operatorIds.add(hlhtTenantOpen.getHlhtOperatorId());
            }
        }

        HlhtTenantOpenExample tenantExample2 = new HlhtTenantOpenExample();
        tenantExample2.createCriteria().andHlhtTenantCodeEqualTo(tenantUser.getTenantCode()).andStatusEqualTo("1").andHlhtTypeEqualTo("0");
        List<HlhtTenantOpen> hlhtTenantOpens1 = hlhtTenantOpenService.selectByExample(tenantExample2);

        for (HlhtTenantOpen hlhtTenantOpen : hlhtTenantOpens1) {
            tenantCodes.add(hlhtTenantOpen.getTenantCode());
        }
        List<String> result = new ArrayList<>(tenantCodes);
        List<Tenant> tenantList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(result)) {
            TenantExample tenantExample = new TenantExample();
            tenantExample.createCriteria().andTenantCodeIn(result);
            tenantList = tenantService.selectByExample(tenantExample);
        }

        List<HlhtOperator> hlhtOperators = new ArrayList<>();

        if (!CollectionUtils.isEmpty(operatorIds)) {
            HlhtOperatorExample example = new HlhtOperatorExample();
            example.createCriteria().andHlhtOperatorIdIn(operatorIds);
            hlhtOperators = hlhtOperatorService.selectByExample(example);
        }
        model.addAttribute("tenant", tenant);
        model.addAttribute("tenantList", tenantList);
        model.addAttribute("hlhtOperators", hlhtOperators);
        return "/templates/invoice/operatorStationInvoice";
    }

    /**
     * 分销商桩群发票统计信息
     * @param stationInvoiceSearch
     */
    @ResponseBody
    @RequestMapping(value = "/listPageCountOperatorStationInvoice", method = RequestMethod.GET)
    public WebPageInfo listPageCountOperatorStationInvoice(StationInvoiceSearch stationInvoiceSearch){
        TenantUser user = ShiroUtils.getSessionUser();
        stationInvoiceSearch.setTenantOperatorCode(user.getTenantOperatorCode());
        if (StringUtil.isNotBlank(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
            if ("tenantCode".equals(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
                stationInvoiceSearch.setUserTenantCode(user.getTenantCode());
            } else {
                HlhtOperator hlhtOperator = hlhtOperatorService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (hlhtOperator != null) {
                    stationInvoiceSearch.setEquipmentOperatorId(hlhtOperator.getHlhtOperatorId());
                }

                Tenant tenant = tenantService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (tenant != null) {
                    stationInvoiceSearch.setEquipmentTenantCode(tenant.getTenantCode());
                }
            }
        }
        PageInfo<StationInvoiceVo> pageInfo=new PageInfo<>();
        if(StringUtil.isNotBlank(stationInvoiceSearch.getOrderType())){
            //个人订单统计
            if ("0".equals(stationInvoiceSearch.getOrderType())){
                pageInfo = tenantInvoiceInfoService.listPageCountOperatorStationInVoice1(stationInvoiceSearch);
            } else if ("1".equals(stationInvoiceSearch.getOrderType())) {
                //企业订单统计
                pageInfo = tenantInvoiceInfoService.listPageCountOperatorStationInVoice2(stationInvoiceSearch);
            } else if ("2".equals(stationInvoiceSearch.getOrderType())) {
                //互联订单统计
                pageInfo = tenantInvoiceInfoService.listPageCountOperatorStationInVoice(stationInvoiceSearch);
            }
        }
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 导出分销商某个站点的详细发票详情
     * @param stationInvoiceSearch
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/exportOperatorStationInvoiceDetail", method = RequestMethod.GET)
    public TResponse exportOperatorStationInvoiceDetail(StationInvoiceSearch stationInvoiceSearch, HttpServletResponse response){
        TenantUser user = ShiroUtils.getSessionUser();
        ExportUtils.setDefaultPaging(stationInvoiceSearch);
        stationInvoiceSearch.setTenantOperatorCode(user.getTenantOperatorCode());
        if (StringUtil.isNotBlank(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
            if ("tenantCode".equals(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
                stationInvoiceSearch.setUserTenantCode(user.getTenantCode());
            } else {
                HlhtOperator hlhtOperator = hlhtOperatorService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (hlhtOperator != null) {
                    stationInvoiceSearch.setEquipmentOperatorId(hlhtOperator.getHlhtOperatorId());
                }

                Tenant tenant = tenantService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (tenant != null) {
                    stationInvoiceSearch.setEquipmentTenantCode(tenant.getTenantCode());
                }
            }
        }
        List<StationInvoiceDetailExportVo> list=new ArrayList<>();
        if (StringUtil.isNotBlank(stationInvoiceSearch.getOrderType())){
            if ("0".equals(stationInvoiceSearch.getOrderType())){
                list=tenantInvoiceInfoService.listOperatorStationInvoiceDetail1(stationInvoiceSearch);
            } else if ("1".equals(stationInvoiceSearch.getOrderType())) {
                list=tenantInvoiceInfoService.listOperatorStationInvoiceDetail2(stationInvoiceSearch);
            } else if ("2".equals(stationInvoiceSearch.getOrderType())) {
                list=tenantInvoiceInfoService.listOperatorStationInvoiceDetail(stationInvoiceSearch);
            }
        }
//        List<StationInvoiceDetailExportVo> list =  tenantInvoiceInfoService.listStationInvoiceDetail(stationInvoiceSearch);

        response.setHeader("Set-Cookie", "fileDownload=true; path=/");

        if (list.size() > 150000) {
            return TResponse.FAIL("最多导出十五万数据");
        }

        if (list.size() >= 100000) {
            String exportData = (String) redisLoader.get("saas:exportData");
            if (exportData != null) {
                return TResponse.FAIL("导出人数过多,请稍后重试");
            }
//            TenantUser user = ShiroUtils.getSessionUser();
            redisLoader.set("saas:exportData", user.getTenantCode(), 1000 * 20);
        }

        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("桩群发票详情列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), StationInvoiceDetailExportVo.class).sheet("桩群发票详情列表").doWrite(list);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("导出桩群发票详情列表", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }

    }

    /**
     * 导出分销商桩群发票
     * @param stationInvoiceSearch
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/exportOperatorStationInvoice", method = RequestMethod.GET)
    public TResponse exportOperatorStationInvoice(StationInvoiceSearch stationInvoiceSearch, HttpServletResponse response){
        TenantUser user = ShiroUtils.getSessionUser();
        ExportUtils.setDefaultPaging(stationInvoiceSearch);
        if (StringUtil.isNotBlank(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
            if ("tenantCode".equals(stationInvoiceSearch.getEquipmentHlhtOperatorId())) {
                stationInvoiceSearch.setUserTenantCode(user.getTenantCode());
            } else {
                HlhtOperator hlhtOperator = hlhtOperatorService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (hlhtOperator != null) {
                    stationInvoiceSearch.setEquipmentOperatorId(hlhtOperator.getHlhtOperatorId());
                }

                Tenant tenant = tenantService.selectByPrimaryKey(stationInvoiceSearch.getEquipmentHlhtOperatorId());
                if (tenant != null) {
                    stationInvoiceSearch.setEquipmentTenantCode(tenant.getTenantCode());
                }
            }
        }
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        if (stationInvoiceSearch.getCount() == 0 || stationInvoiceSearch.getCount() == null) {
            stationInvoiceSearch.setCount(stationInvoiceSearch.getLimit());
        }

        if (stationInvoiceSearch.getCount() > 150000) {
            return TResponse.FAIL("最多导出十五万数据");
        }

        if (stationInvoiceSearch.getCount() >= 100000) {
            String exportData = (String) redisLoader.get("saas:exportData");
            if (exportData != null) {
                return TResponse.FAIL("导出人数过多,请稍后重试");
            }
            redisLoader.set("saas:exportData", user.getTenantCode(), 1000 * 20);
        }
        if(stationInvoiceSearch.getOffset() == null){
            stationInvoiceSearch.setOffset(0);
        }else {
            stationInvoiceSearch.setOffset(stationInvoiceSearch.getLimit() * (stationInvoiceSearch.getOffset() - 1));
        }
        List<StationInvoiceExportVo> list=new ArrayList<>();
        if (StringUtil.isNotBlank(stationInvoiceSearch.getOrderType())){
            if ("0".equals(stationInvoiceSearch.getOrderType())){
                list=tenantInvoiceInfoService.listCountOperatorStationInVoice1(stationInvoiceSearch);
            } else if ("1".equals(stationInvoiceSearch.getOrderType())) {
                list=tenantInvoiceInfoService.listCountOperatorStationInVoice2(stationInvoiceSearch);
            } else if ("2".equals(stationInvoiceSearch.getOrderType())) {
                list=tenantInvoiceInfoService.listCountOperatorStationInVoice(stationInvoiceSearch);
            }
        }
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("桩群发票列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), StationInvoiceExportVo.class).sheet("桩群发票列表").doWrite(list);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("导出桩群发票列表", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }

    }

}
